package com.jiai.cloud.community.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.base.util.IdGenerate;
import com.jiai.cloud.base.util.SpringUtil;
import com.jiai.cloud.community.entity.TSystemCatalog;
import com.jiai.cloud.community.entity.TSystemImg;
import com.jiai.cloud.community.service.ITSystemCatalogService;
import com.jiai.cloud.community.service.ITSystemImgService;
import com.jiai.cloud.config.Config;
import com.jiai.cloud.entity.TGoodsSpac;
import com.jiai.cloud.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 * Created by Administrator on 2017/6/30.
 */
@RestController
@RequestMapping("/api/v1/TSystemCatalog")
public class TSystemCatalogController {

    @Autowired
    private ITSystemCatalogService itSystemCatalogService;

    @Autowired
    private ITSystemImgService iTSystemImgService;

    //创建分组
    @RequestMapping(value = "/catalog",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public  Object  newFolder( String systemCatalog)  {
        TSystemCatalog str=itSystemCatalogService.selectOne(new EntityWrapper<TSystemCatalog>().eq("systemCatalog",systemCatalog));
        if ( str== null){
            String  filePath  =IdGenerate.generateShortUuid();
            String filePath2 = SpringUtil.getBean(Config.class).getFilePath();
            String path=filePath2+"\\"+filePath;
            File file=new File(path);
            if (!file.exists()){
                file.mkdir();
            }
            TSystemCatalog tSystemCatalog=new TSystemCatalog();
            tSystemCatalog.setSystemCatalog(systemCatalog);
            tSystemCatalog.setSystemsaveCatalog(filePath);
            itSystemCatalogService.insert(tSystemCatalog);
            return  RestResultGenerator.genSuccessResult(tSystemCatalog);
        }else {
            return  RestResultGenerator.genErrorResult("分组已经存在");
        }
    }
    //查所有分页显示
    @RequestMapping(value = "/findByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll(@RequestParam("page") Integer page) throws ServiceException {
        try {
            Page<TSystemCatalog> pageSizi= new Page<>(page, 20);
            itSystemCatalogService.selectPage(pageSizi, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(pageSizi));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }
    //修改分组
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object edit(TSystemCatalog tSystemCatalog ){
                itSystemCatalogService.updateById(tSystemCatalog);
            return  RestResultGenerator.genSuccessResult("修改成功");

    }
    //删除分组和下面的图片
    @RequestMapping(value = "/del", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteId( TSystemCatalog tSystemCatalog) throws ServiceException{
        String filePath = SpringUtil.getBean(Config.class).getFilePath();//获取系统当前保存目录
        String Path=filePath+"\\"+tSystemCatalog.getSystemsaveCatalog();
        File file=new File(Path);
        UploadUtil.deleteFile(file);
        itSystemCatalogService.deleteById(tSystemCatalog.getSystemCatalogId());
        iTSystemImgService.delete(new EntityWrapper<TSystemImg>().eq("systemCatalogId",tSystemCatalog.getSystemCatalogId()));
        return RestResultGenerator.genSuccessResult("删除成功");
    }
    @RequestMapping(value = "/all",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object ByAll() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itSystemCatalogService.selectList(null));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }
}
